/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.Field;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author softviet
 */
public class Utility {

    public static void SetDataSourceInCombo(List<?> list, JComboBox combobox) {
        JComboBoxBinding comboboxBinding = SwingBindings.createJComboBoxBinding(AutoBinding.UpdateStrategy.READ, list, combobox);
        comboboxBinding.bind();
    }

    public static void CreateColumnsFromDB(Class<?> clazz, List<?> list, JTable table, String[] tableName, String[] ignoreField) {
        BindingGroup bindingGroup = new BindingGroup();
        JTableBinding tableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ_WRITE, list, table);
        JTableBinding.ColumnBinding columBinding;
        Field fields[] = clazz.getDeclaredFields();
        int i = 0;
        //Arrays.asList(tableName).con
        for (Field f : fields) {
            Class<?> clazzOfField = toWrapper(f.getType());
            String name = f.getName();
            if (clazzOfField == null || name.contains("isDelete") || (ignoreField != null && Arrays.asList(ignoreField).contains(name))) {
                continue;
            }
            columBinding = tableBinding.addColumnBinding(ELProperty.create("${" + name + "}"));
            columBinding.setColumnName(tableName[i]);
            i++;
            columBinding.setColumnClass(clazzOfField);
            //if(!f.getName().toLowerCase().contains("isdelete")){
            columBinding.setEditable(false);
            //}
        }
        bindingGroup.addBinding(tableBinding);
        bindingGroup.bind();
    }

    public static List<?> GetSelectedRows(JTable table, List<?> list) {
        int sodong[] = table.getSelectedRows();
        Object[] temp = new Object[sodong.length];
        for (int i = 0; i < sodong.length; i++) {
            sodong[i] = table.convertRowIndexToModel(sodong[i]);
        }
        int k = 0;
        for (int i = 0; i < sodong.length; i++) {
            temp[k] = list.get(sodong[i]);
            k++;
        }
        return Arrays.asList(temp);
    }

    public static void RemoveSelectedRows(Class<?> clazz, JTable table, List<?> list, String[] tableName, String[] ignoreField) {
        int sodong[] = table.getSelectedRows();
        int dem = 0;
        for (int i = 0; i < sodong.length; i++) {
            sodong[i] = table.convertRowIndexToModel(sodong[i]);
        }
        for (int i = 0; i < sodong.length; i++) {
            list.remove(sodong[i] - dem);
            dem++;
        }
        CreateColumnsFromDB(clazz, list, table, tableName, ignoreField);
    }

    public static void ClearControlInForm(JInternalFrame form) {
        Component controls[] = form.getContentPane().getComponents();
        for (Component control : controls) {
            if (control instanceof JTextField) {
                ((JTextField) control).setText(null);
            } else if (control instanceof JComboBox) {
                ((JComboBox) control).setSelectedIndex(-1);
            } else if (control instanceof JFormattedTextField) {
                ((JFormattedTextField) control).setText(null);
            } else if (control instanceof JXDatePicker) {
                ((JXDatePicker) control).setDate(null);
            }
        }
    }

    public static void ClearControlInPanel(JPanel panel) {
        Component controls[] = panel.getComponents();
        for (Component control : controls) {
            if (control instanceof JTextField) {
                ((JTextField) control).setText(null);
            } else if (control instanceof JComboBox) {
                ((JComboBox) control).setSelectedIndex(-1);
            } else if (control instanceof JFormattedTextField) {
                ((JFormattedTextField) control).setText(null);
            } else if (control instanceof JXDatePicker) {
                ((JXDatePicker) control).setDate(null);
            }
        }
    }

    public static void FormatNumberStringTextfield(JTextField txt) {
        txt.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (!e.isActionKey() && e.getKeyCode() != 127 && e.getKeyCode() != 8) {
                    if (e.getKeyChar() < '0' || e.getKeyChar() > '9') {
                        e.consume();
                    }
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }

    public static void FormatNumberTextfield(JFormattedTextField txt) {
        txt.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(NumberFormat.getNumberInstance()), new NumberFormatter(NumberFormat.getNumberInstance()), new NumberFormatter(NumberFormat.getNumberInstance()), new NumberFormatter(NumberFormat.getNumberInstance())));
        txt.setHorizontalAlignment(JTextField.RIGHT);
        txt.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (!e.isActionKey() && e.getKeyCode() != 127 && e.getKeyCode() != 8) {
                    if (e.getKeyChar() < '0' || e.getKeyChar() > '9') {
                        e.consume();
                    }
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }

    public static void FormatDecimalTextfield(JFormattedTextField txt) {
        txt.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(NumberFormat.getNumberInstance()), new NumberFormatter(NumberFormat.getNumberInstance()), new NumberFormatter(NumberFormat.getNumberInstance()), new NumberFormatter(NumberFormat.getNumberInstance())));
        txt.setHorizontalAlignment(JTextField.RIGHT);
        txt.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (!e.isActionKey() && e.getKeyCode() != 127 && e.getKeyCode() != 8 && e.getKeyChar() != '.') {
                    if (e.getKeyChar() < '0' || e.getKeyChar() > '9') {
                        e.consume();
                    }
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }

    public static Class toWrapper(Class clazz) {
        if (clazz.equals(Number.class.isAssignableFrom(clazz))) {
            return clazz;
        }
        if (clazz.equals(int.class)) {
            return Integer.class;
        }
        if (clazz.equals(Integer.class)) {
            return Integer.class;
        }
        if (clazz.equals(long.class)) {
            return Long.class;
        }
        if (clazz.equals(Long.class)) {
            return Long.class;
        }
        if (clazz.equals(boolean.class)) {
            return Boolean.class;
        }
        if (clazz.equals(Float.class)) {
            return Float.class;
        }
        if (clazz.equals(double.class)) {
            return Double.class;
        }
        if (clazz.equals(Double.class)) {
            return Double.class;
        }
        if (clazz.equals(short.class)) {
            return Short.class;
        }
        if (clazz.equals(String.class)) {
            return String.class;
        }
        if (clazz.equals(Date.class)) {
            return Date.class;
        }
        if (clazz.equals(Boolean.class)) {
            return Boolean.class;
        }

        return null;
    }
}
